package com.gugee.tiktok.common.model.dal.hashtag.impl;

import com.gugee.tiktok.common.model.dal.hashtag.HashtagExtRepository;
import com.gugee.tiktok.common.model.mysql.Hashtag;
import com.gugee.tiktok.common.model.spider.aweme.Text_extra;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: luolifeng
 * Date: 2020-10-20 16:46
 */
public class HashtagExtRepositoryImpl implements HashtagExtRepository {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public void batchUpdate(List<Hashtag> hashtags) {

    }

    @Override
    public void batchSaveHashTag(List<Text_extra> hashtags){
        long seconds = DateTimeUtils.currentTimeSecond();
        /**
         * todo 收录标签，若想解耦，可将hid发送到消息队列批量消费
         */
        jdbcTemplate.batchUpdate(
                "INSERT INTO `tiktok`.`hashtag`" +
                        "(`id`, `hid`," +
                        " `create_time`, " +
                        "`update_time`, `create_by`, `update_by`, `type`, `hashtag_name`, `is_commerce`)" +
                        " VALUES(?,?,?,?,?,?,?,?,?)"
                , new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                        Text_extra hashTag = hashtags.get(i);
                        preparedStatement.setLong(1, 0);
                        preparedStatement.setLong(2, hashTag.getHashtag_id());
                        preparedStatement.setLong(3,seconds);
                        preparedStatement.setLong(4, seconds);
                        preparedStatement.setString(5, "batch save");
                        preparedStatement.setString(6, "batch save");
                        preparedStatement.setInt(7,hashTag.getType() );
                        preparedStatement.setString(8, hashTag.getHashtag_name());
                        preparedStatement.setBoolean(9,hashTag.isCommerce());
                    }

                    @Override
                    public int getBatchSize() {
                        return hashtags.size();
                    }
                });
    }
}
